﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket_Desktop.BL;

namespace ElectronicsMarket_Desktop
{
    public partial class frmQuangCao : Form
    {
        BillboardBL billboardbl;
        public frmQuangCao()
        {
            InitializeComponent();
        }

        private void frmQuangCao_Load(object sender, EventArgs e)
        {
            dataBind();
        }
        public void dataBind()
        {
            billboardbl = new BillboardBL();
            grdquangcao.AutoGenerateColumns = false;
            grdquangcao.DataSource = billboardbl.getAllBillBoard();
        }

        private void btnchapnhan_Click(object sender, EventArgs e)
        {
            billboardbl = new BillboardBL();
            PictureCovert piconvert;
           
                billboardbl.Billboard.BILLBOAD_ID = Convert.ToInt32(txtmaquangcao.Text);
                billboardbl.Billboard.BILLBOAD_DESCRIPTION = txtmota.Text;
                billboardbl.Billboard.BILLBOAD_NAME = txtTen.Text;
                billboardbl.Billboard.BILLBOAD_POSTED_AT = dtngaydualen.Value;
                billboardbl.Billboard.BILLBOAD_STAFF_ID = StaticParameter.Idnhanvien;
                billboardbl.Billboard.BILLBOAD_LINK_TO = txtdiachiweb.Text;

            
           
            if (rdioThem.Checked == true)
            {
                billboardbl.Billboard.BILLBOAD_ID = Convert.ToInt32(billboardbl.getLastBillboardiId() + 1);
                piconvert = new PictureCovert(picAnh.Image);
                piconvert.PicturrConvertToBinary();
                billboardbl.Billboard.BILLBOAD_IMAGE = piconvert.Picture_byte;
                //
                billboardbl.insertBillboard();
                dataBind();
            }
            if (rdioCapNhat.Checked == true)
            {
                piconvert = new PictureCovert(picAnh.Image);
                piconvert.PicturrConvertToBinary();
                billboardbl.Billboard.BILLBOAD_IMAGE = piconvert.Picture_byte;
                //
                billboardbl.updateBillboard();
                dataBind();
            }
            if (rdioXoa.Checked == true)
            {
                billboardbl.deleteBillboard();
                dataBind();
            }
        }

        private void btnUpHinh_Click(object sender, EventArgs e)
        {
            if (openAnh.ShowDialog() == DialogResult.OK)
            {
                picAnh.Image = System.Drawing.Image.FromFile(openAnh.FileName);

            }
        }

        private void grdquangcao_SelectionChanged(object sender, EventArgs e)
        {
            txtmaquangcao.Text = grdquangcao.CurrentRow.Cells[0].Value.ToString();
            txtTen.Text = grdquangcao.CurrentRow.Cells["coltennhanhieu"].Value.ToString();
            txtmota.Text = grdquangcao.CurrentRow.Cells["colmotaquangcao"].Value.ToString();
            txtdiachiweb.Text = grdquangcao.CurrentRow.Cells["coldiachiweb"].Value.ToString();
            dtngaydualen.Text = Convert.ToDateTime( grdquangcao.CurrentRow.Cells["colngaydua"].Value).ToShortDateString();
            PictureCovert picconver = new PictureCovert((byte[])grdquangcao.CurrentRow.Cells["colanhnhanhieu"].Value);
            picconver.BinaryconvertToPicture();
            picAnh.Image = picconver.Pic;
            //
            this.picAnh.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;


        }
    }
}
